package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.common.R;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", employee.getUsername());
        Employee employee1 = employeeService.getOne(queryWrapper);
        if (employee1 == null) {
            return R.error("用户名或密码错误");
        } else {
            if (!DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()).equals(employee1.getPassword())) {
                return R.error("用户名或密码错误");
            } else {
                if (employee1.getStatus().equals(0)) {
                    return R.error("员工已被禁用");
                }else {
                    request.getSession().setAttribute("employee",employee1.getId());
                    return R.success(employee1);
                }
            }
        }
    }

    @PostMapping("/logout")
    public R<Employee> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success(null);
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){

        //设置分页过滤器
        Page page1 = new Page(page,pageSize);

        //设置条件过滤器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //设置过滤条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        //设置降序
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(page1, lambdaQueryWrapper);
        return R.success(page1);
    }

   //添加操作
    @PostMapping
    public R<String> saveEmployee(HttpServletRequest request,@RequestBody Employee employee){
        //密码加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //设置系统时间为当前时间
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        Long employeeId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(employeeId);
        employee.setUpdateUser(employeeId);*/

        employeeService.save(employee);
        return R.success("员工添加成功");
    }

    //员工的禁用和启动
    @PutMapping
    public R<String> updateEmployee(HttpServletRequest request,@RequestBody Employee employee){
        /*Long employeeId = (Long)request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(employeeId);*/
        employeeService.updateById(employee);
        return R.success("员工修改成功");                                                                                                       }

    //编辑员工
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
}
